//Promise.retry 超时重新请求，并在重试一定次数依然失败时输出缓存内容
// Promise.retry 超时重新请求，并在重试一定次数依然失败时输出缓存内容
function retry(promiseFn, retries = 3, delay = 1000, cacheData = null) {
  return new Promise((resolve, reject) => {
    const attempt = (remainingRetries) => {
      promiseFn()
        .then(resolve)
        .catch((error) => {
          if (remainingRetries <= 0) {
            // 重试次数用尽，返回缓存内容
            if (cacheData !== null) {
              resolve(cacheData);
            } else {
              reject(error);
            }
          } else {
            // 延迟后重试
            setTimeout(() => {
              attempt(remainingRetries - 1);
            }, delay);
          }
        });
    };

    attempt(retries);
  });
}

// 使用示例
// retry(() => fetch('/api/data'), 3, 1000, cachedData)
//   .then(result => console.log('Success:', result))
//   .catch(error => console.error('Failed:', error));


